﻿//  LunarCalendar
//
//  Created by mac on 13-8-27.
//  Copyright (c) 2013年 caobo. All rights reserved.

#import "LunarCalendar.h"
//数组存入阴历1900年到2100年每年中的月天数信息，
//阴历每月只能是29或30天，一年用12（或13）个二进制位表示，对应位为1表30天，否则为29天

@implementation LunarCalendar

static LunarCalendar *_insatance;

+ (instancetype)shareInstance
{
//    static dispatch_once_t dispatch_onces;
//    if (_insatance == nil) {
//        dispatch_once(&dispatch_onces, ^{
//            _insatance = [[LunarCalendar alloc] init];
//        });
//    }
    return [[LunarCalendar alloc] init];
}
-(id)init
{
    self = [super init];
    
	
	
	SolarTerms = [NSArray arrayWithObjects:@"立春", @"雨水", @"惊蛰", @"春分", @"清明", @"谷雨", @"立夏", @"小满", @"芒种", @"夏至", @"小暑", @"大暑", @"立秋", @"处暑", @"白露", @"秋分", @"寒露", @"霜降", @"立冬", @"小雪", @"大雪", @"冬至", @"小寒", @"大寒", nil];
	
	arrayMonth = [NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月",  @"十月", @"冬月", @"腊月", nil];
	
	arrayDay = [NSArray arrayWithObjects:@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
    
    _ChineseHoliday = [NSDictionary dictionaryWithObjectsAndKeys:
                       @"春节", @"01|01",
                       @"元宵", @"01|15",
                       @"端午", @"05|05",
                       @"七夕", @"07|07",
                       @"中元", @"07|15",
                       @"中秋", @"08|15",
                       @"重阳", @"09|09",
                       @"腊八", @"12|08",
                       @"小年", @"12|24",
         
                       nil];
    _GregorianHoliday = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"元旦", @"0101",
                         @"情人节", @"0214",
                         @"妇女节", @"0308",
                         @"消费者权益日", @"0315",
                         @"愚人节", @"0401",
                         @"国际劳动节", @"0501",
                         @"中国五四青年节", @"0504",
                         @"世界红十字日", @"0508",
                         @"国际护士节", @"0512",
                         @"国际母亲节", @"0520",
                         @"国际儿童节", @"0601",
                         @"国际父亲节", @"0630",
                         @"建党节", @"0701",
                         @"建军节", @"0801",
                         @"国庆节", @"1001",
                         @"平安夜", @"1224",
                         @"圣诞节", @"1225",
                         nil];

    
    if(!self)
    {
        return nil;
    }
	
	return self;
}

-(void)loadWithDate:(NSDate *)adate
{
    if (adate == nil){
        return;
    };
	
    NSCalendar *GregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *GregorianComponents = [GregorianCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:adate];
    
    
    NSCalendar *ChineseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
    NSDateComponents *ChineseComponents = [ChineseCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:adate];
    NSRange rng = [ChineseCalendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:adate];
    NSUInteger numberOfDaysInMonth = rng.length;

    
    
    if (ChineseComponents.day == 1) {
        self.DayLunar = arrayMonth[ChineseComponents.month - 1];
    } else {
        self.DayLunar = arrayDay[ChineseComponents.day - 1];
    }
    if (ChineseComponents.month == 12 && ChineseComponents.day == numberOfDaysInMonth) {
        self.DayLunar = @"除夕";
    } else {
       NSString *ChineseHoliday = [self getChineseHoliday:ChineseComponents.month day:ChineseComponents.day];
        if (ChineseHoliday) {
            self.DayLunar = ChineseHoliday;
        }
    }
    
    NSString *gregorianHoliday = [self getWorldHoliday:GregorianComponents.month day:GregorianComponents.day];
    if (gregorianHoliday) {
        self.DayLunar = gregorianHoliday;
    }
}


-(NSString *)getChineseHoliday:(NSInteger)aMonth day:(NSInteger)aDay
{
    
    return [_ChineseHoliday objectForKey:[NSString stringWithFormat:@"%02zd|%02zd",aMonth,aDay]];
}

-(NSString *)getWorldHoliday:(NSInteger)aMonth day:(NSInteger)aDay
{
 
    NSString *monthDay=[NSString stringWithFormat:@"%02zd%02zd",aMonth,aDay] ;
   
    return [_GregorianHoliday objectForKey:monthDay];
}




-(NSString *)DayLunar
{
	return _DayLunar;
}



@end


//方法获取农历数具包的方法
@implementation NSDate (LunarCalendar)

/****************************************************
 *@Description:获得NSDate对应的中国日历（农历）的NSDate
 *@Params:nil
 *@Return:NSDate对应的中国日历（农历）的LunarCalendar
 ****************************************************/
- (LunarCalendar *)chineseCalendarDate
{
    LunarCalendar *lunarCalendar = [LunarCalendar shareInstance];
    [lunarCalendar loadWithDate:self];
    return lunarCalendar;
//    return nil;
}

@end